
#import "CalculationDetailsViewController.h"
#import "UINavigationItem+InfoButton.h"

@interface CalculationDetailsViewController ()

@property (weak, nonatomic) IBOutlet UIWebView *webView;

@property (weak, nonatomic) SavingsCalculator *savingsCalculator;
@property (assign, nonatomic) SavingsCalculatorLampCalculation *newwLampCalculation;

@end


@implementation CalculationDetailsViewController

- (instancetype)initWithSavingsCalculator:(SavingsCalculator *)savingsCalculator substituteIndex:(NSInteger)substituteIndex
{
    self = [super init];
    if (self) {
        _savingsCalculator = savingsCalculator;
        _newwLampCalculation = savingsCalculator.substCalculation[substituteIndex];
    }
    return self;
}

#define FULL_RANGE(s) ((NSRange){0, s.length})

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = _(@"Så har vi räknat");
    self.view.backgroundColor = [UIColor whiteColor];
    
    // Load template
    NSString *locale = @"sv";
    NSString *file = [locale stringByAppendingFormat:@"_%@", @"Beskrivning_av_berakning"];
    NSMutableString *html = [[NSString stringWithContentsOfFile:[[NSBundle mainBundle]
                                                                 pathForResource:file
                                                                 ofType:@"html"]
                                                       encoding:NSUTF8StringEncoding error:nil] mutableCopy];
    [html replaceOccurrencesOfString:@"../" withString:@"" options:0 range:FULL_RANGE(html)];
    
    // Formatters
    NSNumberFormatter *separatedNumberFormatter = [[NSNumberFormatter alloc] init];
    separatedNumberFormatter.usesGroupingSeparator = YES;
    separatedNumberFormatter.groupingSeparator = @"&nbsp;";
    separatedNumberFormatter.groupingSize = 3;
    
    NSNumberFormatter *commaNumberFormatter = [[NSNumberFormatter alloc] init];
    commaNumberFormatter.decimalSeparator = @",";
    commaNumberFormatter.minimumFractionDigits = 2;
    
    // Substitutions
    SavingsCalculator *calculator = self.savingsCalculator;
    SavingsCalculatorLampCalculation *oldLampCalculation = calculator.oldLampCalculation;
    SavingsCalculatorLampCalculation *newLampCalculation = self.newwLampCalculation;
    
    NSDictionary *substitutions = @{
                                    @"energy_price": [commaNumberFormatter stringFromNumber:@(calculator.energyPriceKWh)],
                                    @"calculation_hours": [separatedNumberFormatter stringFromNumber: @(calculator.calculationHours)],
                                    @"calculation_years": [@(calculator.calculationYears) stringValue],
                                    @"luminous_flux": [NSString stringWithFormat:@"%@–%@",
                                                       [separatedNumberFormatter stringFromNumber:@([calculator.luminousFluxRange[0] intValue])],
                                                       [separatedNumberFormatter stringFromNumber:@([calculator.luminousFluxRange[1] intValue])]],
                                    
                                    @"new_lamp_name": newLampCalculation.lampType.lowercaseName,
                                    @"new_lamp_price": [separatedNumberFormatter stringFromNumber:@(newLampCalculation.price)],
                                    @"new_life_length": [separatedNumberFormatter stringFromNumber:@(newLampCalculation.lampType.lifeExpectancy)],
                                    @"new_effect": [@(round(newLampCalculation.effect)) stringValue],
                                    @"new_efficiency_factor": [NSString stringWithFormat:@"%.2f", newLampCalculation.lampType.powerNeedRelativeClassic],
                                    @"new_energy_cost": [separatedNumberFormatter stringFromNumber:@(newLampCalculation.usedEnergyCost)],
                                    @"new_lamps_needed_w_unit": [self lampsNeededWithUnit:newLampCalculation.lampsNeeded],
                                    @"new_lamps_purchase_cost": [separatedNumberFormatter stringFromNumber: @(newLampCalculation.lampsPurchaseCost)],
                                    @"new_total_cost": [separatedNumberFormatter stringFromNumber:@(newLampCalculation.totalCost)],
                                    
                                    @"old_lamp_name": oldLampCalculation.lampType.lowercaseName,
                                    @"old_lamp_price": [separatedNumberFormatter stringFromNumber:@(oldLampCalculation.price)],
                                    @"old_life_length": [separatedNumberFormatter stringFromNumber:@(oldLampCalculation.lampType.lifeExpectancy)],
                                    @"old_effect": [@(oldLampCalculation.effect) stringValue],
                                    @"old_efficiency_factor": [NSString stringWithFormat:@"%.2f", oldLampCalculation.lampType.powerNeedRelativeClassic],
                                    @"old_energy_cost": [separatedNumberFormatter stringFromNumber:@(oldLampCalculation.usedEnergyCost)],
                                    @"old_lamps_needed_w_unit": [self lampsNeededWithUnit:oldLampCalculation.lampsNeeded],
                                    @"old_lamps_purchase_cost": [separatedNumberFormatter stringFromNumber:@(oldLampCalculation.lampsPurchaseCost)],
                                    @"old_total_cost": [separatedNumberFormatter stringFromNumber:@(oldLampCalculation.totalCost)],
                                    
                                    @"total_savings": [separatedNumberFormatter stringFromNumber:@(newLampCalculation.totalSavings)],
                                    @"total_savings_exact": [separatedNumberFormatter stringFromNumber:@(newLampCalculation.totalSavingsExact)]
                                    };
    
    [substitutions enumerateKeysAndObjectsUsingBlock:^(NSString *placeholder, NSString *word, BOOL *stop) {
        placeholder = [NSString stringWithFormat:@"$%@$", placeholder];
        [html replaceOccurrencesOfString:placeholder withString:word options:0 range:FULL_RANGE(html)];
    }];
    
    self.webView.dataDetectorTypes = UIDataDetectorTypeNone;
    [self.webView loadHTMLString:html baseURL:[NSBundle mainBundle].bundleURL];
}

-(NSString *)lampsNeededWithUnit:(CGFloat)number {
    NSNumberFormatter *commaNumberFormatter = [[NSNumberFormatter alloc] init];
    commaNumberFormatter.decimalSeparator = @",";
    commaNumberFormatter.minimumFractionDigits = 0;
    commaNumberFormatter.maximumFractionDigits = 1;
    
    if (number == 1) {
        return @"1 lampa";
    } else {
        return [[commaNumberFormatter stringFromNumber:@(number)] stringByAppendingString:@" lampor"];
    }
}

@end
